import request from '@/utils/request'

// 查询VIP套餐列表
export function listVipPackage(query) {
  return request({
    url: '/checksystem/package/list',
    method: 'get',
    params: query
  })
}

// 查询VIP套餐详细
export function getVipPackage(id) {
  return request({
    url: '/checksystem/package/' + id,
    method: 'get'
  })
}

// 新增VIP套餐
export function addVipPackage(data) {
  return request({
    url: '/checksystem/package',
    method: 'post',
    data: data
  })
}

// 修改VIP套餐
export function updateVipPackage(data) {
  return request({
    url: '/checksystem/package',
    method: 'put',
    data: data
  })
}

// 删除VIP套餐
export function delVipPackage(id) {
  return request({
    url: '/checksystem/package/' + id,
    method: 'delete'
  })
}

// 批量删除VIP套餐
export function delVipPackages(ids) {
  return request({
    url: '/checksystem/package/' + ids,
    method: 'delete'
  })
}

// 检查用户是否已购买过指定类型的套餐
export function hasUserPurchasedPackage(userId, packageType) {
  return request({
    url: `/checksystem/package/haspackage/${userId}/${packageType}`,
    method: 'get'
  })
}

// 充值VIP套餐
export function rechargeVipPackage(data) {
  // 确保字段名称与后端保持一致
  const requestData = {
    ...data
  };
  
  // 如果是传递的snake_case格式，转为camelCase
  if (data.user_id) {
    requestData.userId = data.user_id;
    delete requestData.user_id;
  }
  
  if (data.package_type) {
    requestData.packageType = data.package_type;
    delete requestData.package_type;
  }
  
  if (data.total_quota) {
    requestData.totalQuota = data.total_quota;
    delete requestData.total_quota;
  }
  
  if (data.used_quota) {
    requestData.usedQuota = data.used_quota;
    delete requestData.used_quota;
  }
  
  if (data.start_time) {
    requestData.startTime = data.start_time;
    delete requestData.start_time;
  }
  
  if (data.end_time) {
    requestData.endTime = data.end_time;
    delete requestData.end_time;
  }
  
  if (data.is_deleted !== undefined) {
    requestData.isDeleted = data.is_deleted;
    delete requestData.is_deleted;
  }
  
  console.log('充值请求数据:', requestData);
  
  return request({
    url: '/checksystem/package/recharge',
    method: 'post',
    data: requestData
  })
}

// 获取弹窗详情
export function getPopupDetails(query) {
  return request({
    url: '/checksystem/details/list',
    method: 'get',
    params: query
  })
}

// 添加弹窗详情
export function addPopupDetails(data) {
  return request({
    url: '/checksystem/details',
    method: 'post',
    data: data
  })
}

// 导出VIP套餐
export function exportVipPackage(query) {
  return request({
    url: '/checksystem/package/export',
    method: 'post',
    params: query
  })
}

// 获取用户套餐信息，如果不存在则返回空的套餐记录
export function getUserPackageOrEmpty(userId) {
  return request({
    url: `/checksystem/package/userPackageOrEmpty/${userId}`,
    method: 'get'
  })
}

// 获取用户最新套餐信息
export function getUserLatestPackage(userId) {
  return request({
    url: `/checksystem/package/latest/${userId}`,
    method: 'get'
  })
}    